package application.exportimport;

import dao.inte.ProductRepository;
import domain.shop.Product;
import org.springframework.beans.factory.annotation.Autowired;

import javax.inject.Singleton;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Generate XML file of specification of heureka.cz server.
 * Its same as zbozi.cz. I separated it for future adjusting, if any.
 */
@Singleton
public class GeneratorHeurekaCz extends AbstractXmlGenerator {

    @Autowired
    private ProductRepository productRepository;
    private static final String XML_FILE_NAME = "heurekaCzXml";

    public GeneratorHeurekaCz() {
    }

    protected File createFile() throws IOException {
        String fileName = XML_FILE_NAME + AbstractXmlGenerator.XML_FILE_SUFFIX;
        File file = new File(directory + fileName);
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        return file;
    }

    protected AbstractXmlWrapper createAndFillXMLWrapper() {
        HeurekaCzXMLWrapper xmlWrapper = new HeurekaCzXMLWrapper();
        xmlWrapper.setProducts(productRepository.findAll());
        return xmlWrapper;
    }

    @XmlRootElement(name = "SHOP")
    private static class HeurekaCzXMLWrapper extends AbstractXmlWrapper {

        List<Product> products;

        public HeurekaCzXMLWrapper() {
        }

        @XmlElement(name = "SHOPITEM")
        public List<Product> getProducts() {
            return products;
        }

        public void setProducts(List<Product> products) {
            this.products = products;
        }
    }
}
